package com.itheima.reggie.controller;


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;

    //通过分类id查找相应对菜
    @GetMapping("/dish/list")
    public ResultInfo findDish(String name,Long categoryId,@RequestParam(defaultValue = "1") Integer status){

        List<Dish> dishList  = (List<Dish>) redisTemplate.opsForValue().get("dish_" + categoryId);

        //判断如果为空，则进行数据库查询后在存到redis缓存中
        if (CollectionUtil.isNotEmpty(dishList)){
            return ResultInfo.success(dishList);
        }else {
            dishList = dishService.findDishList(name,categoryId,status);
            redisTemplate.opsForValue().set("dish_" + categoryId , dishList);
            return ResultInfo.success(dishList);
        }
    }
}
